<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="mysql_database, java,nodejs">
    <meta name="description" content="create database ShareBook;
use ShareBook;


笔记Mysql 查看帮助? contents
? data types
? create
? create table
? int
? show
表修改">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>mysql_database | libo 工作学习记录</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/highlight/styles/monokai.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    
    <script src="/libs/jquery/jquery.min.js"></script>
    <script data-ad-client="ca-pub-6820955780229803" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper head-container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">libo 工作学习记录</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">libo 工作学习记录</div>
        <div class="logo-desc">
            
            后台开发、编程、学习记录
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			Contact
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
    </ul>
</div>

        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/0.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">mysql_database</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/mysql/">
                                <span class="chip bg-color">mysql</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-01-13
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2019-01-13
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    1.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    7 分
                </div>
                
				
                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
            
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <pre><code>create database ShareBook;
use ShareBook;</code></pre>
<a id="more"></a>

<h2 id="笔记"><a href="#笔记" class="headerlink" title="笔记"></a>笔记</h2><h3 id="Mysql-查看帮助"><a href="#Mysql-查看帮助" class="headerlink" title="Mysql 查看帮助"></a>Mysql 查看帮助</h3><pre><code>? contents
? data types
? create
? create table
? int
? show</code></pre>
<h3 id="表修改"><a href="#表修改" class="headerlink" title="表修改"></a>表修改</h3><ul>
<li>外键支持</li>
</ul>
<p>mysql只有InnoDB 引擎支持外键，所以可以在建表最后添加 engine=InnoDB 来更改默认引擎，也可以通过修改： alter table user engine = innodb;然后通过下面代码增加约束</p>
<pre><code>alter table Admin add constraint foreign key (username) references User(username) on delete restrict on update cascade;</code></pre>
<ul>
<li><p>查看表的详细描述信息</p>
<p>  show create table user \G;</p>
</li>
<li><p>unique 字段约束设置</p>
<p>  alter table user add unique(user_email);</p>
</li>
<li><p>添加外键约束</p>
<p>  alter table Admin add constraint foreign key (username) references User(username);</p>
</li>
<li><p>删除外键</p>
<p>  alter table user drop foreign key fk_user_1;</p>
</li>
<li><p>修改字段</p>
<p>  alter table user modify user_email varchar(21);</p>
</li>
<li><p>添加列</p>
<p>  alter table user add column age int(3);</p>
</li>
<li><p>删除字段</p>
<p>  alter table user drop column age;</p>
</li>
<li><p>字段改名</p>
<p>  alter table user change age age1 int(4);</p>
</li>
<li><p>修改字段顺序</p>
<p>  alter table user add birth date after ename;<br>  alter table user modify age int(3) first;</p>
</li>
<li><p>表改名</p>
<p>  alter table user rename users;</p>
</li>
</ul>
<h3 id="数据操作"><a href="#数据操作" class="headerlink" title="数据操作"></a>数据操作</h3><h4 id="插入"><a href="#插入" class="headerlink" title="插入"></a>插入</h4><ul>
<li><p>插入多条记录</p>
<p>  insert into user (username,pswd)<br>  values<br>  (“warmfrog”, “159491”),<br>  (“warmfrog2”, “159492”);</p>
</li>
</ul>
<h4 id="更新"><a href="#更新" class="headerlink" title="更新"></a>更新</h4><ul>
<li><p>更新多表记录</p>
<p>  update user a, book b set a.username=”warmfrog11”, b.book_name=”Software” where a.user_id=book.book_owner_id;</p>
</li>
</ul>
<h4 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h4><ul>
<li><p>去重显示</p>
<p>  select distinct book_name from book;</p>
</li>
<li><p>排序</p>
<p>  select * from user order by user_id;</p>
</li>
<li><p>显示前3条记录</p>
<p>  select * from user order by user_id, register_time desc limit 3;</p>
</li>
</ul>
<p>limit 和 order by 一起配合进行分页查询</p>
<h4 id="删除多条记录"><a href="#删除多条记录" class="headerlink" title="删除多条记录"></a>删除多条记录</h4><pre><code>delete from User where username in (&#39;warmfrog10&#39;,&#39;warmfrog9&#39;);</code></pre>
<h2 id="数据表"><a href="#数据表" class="headerlink" title="数据表"></a>数据表</h2><h3 id="user"><a href="#user" class="headerlink" title="user"></a>user</h3><p>   create table user(<br>        user_id bigint auto_increment,<br>        nickname varchar(20),<br>        password varchar(20),<br>        address varchar(50),<br>        register_time timestamp,<br>        phone_number varchar(11) unique,<br>        user_email varchar(20) unique,<br>        primary key (user_id)<br>    )engine=InnoDB character set=utf8;</p>
<h3 id="book"><a href="#book" class="headerlink" title="book"></a>book</h3><pre><code>create table book(
    book_id bigint auto_increment,
    order_id bigint,
    book_owner_id bigint,
    book_name varchar(30),
    book_author varchar(40),
    book_author2 varchar(40),
    book_translator varchar(40),
    book_intro varchar(500),
    book_publish_time timestamp,
    book_publisher varchar(20),
    book_edition tinyint,
    book_isbn varchar(20),
    book_rent_price float,
    book_buy_price float,
    foreign key(book_owner_id) references user(user_id) on delete cascade on update cascade,
    foreign key(order_id) references orders(order_id),
    primary key(book_id)
  )engine=InnoDB character set=utf8;</code></pre>
<h3 id="orders"><a href="#orders" class="headerlink" title="orders"></a>orders</h3><pre><code>create table orders(
    order_id bigint auto_increment,
    start_time timestamp,
    trade_type varchar(10),
    current_status varchar(10),
    deliver_id bigint,
    customer_id bigint,
    primary key(order_id)
  )engine=InnoDB character set=utf8;</code></pre>
<h3 id="deliver"><a href="#deliver" class="headerlink" title="deliver"></a>deliver</h3><p>   create table deliver(<br>        deliver_id bigint auto_increment,<br>        deliver_order_id bigint,<br>        deliver_status varchar(10),<br>        deliver_method varchar(10),<br>        foreign key(deliver_order_id) references orders(order_id) on delete restrict on update cascade,<br>        primary key (deliver_id)<br>      )engine=InnoDB character set=utf8;</p>
<p> <a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/8.0/en/">MySQL 8.0 Reference Manual</a></p>
<h2 id="数据库操作"><a href="#数据库操作" class="headerlink" title="数据库操作"></a>数据库操作</h2><h4 id="连接到数据库"><a href="#连接到数据库" class="headerlink" title="连接到数据库"></a>连接到数据库</h4><pre><code> mysql -h localhost -u warmfrog -p</code></pre>
<h4 id="查看数据库"><a href="#查看数据库" class="headerlink" title="查看数据库"></a>查看数据库</h4><pre><code> show databases;

 select database();</code></pre>
<h4 id="使用数据库"><a href="#使用数据库" class="headerlink" title="使用数据库"></a>使用数据库</h4><pre><code> user test</code></pre>
<h4 id="创建数据库"><a href="#创建数据库" class="headerlink" title="创建数据库"></a>创建数据库</h4><pre><code> create database *menagerie*;</code></pre>
<h2 id="表操作"><a href="#表操作" class="headerlink" title="表操作"></a>表操作</h2><h4 id="创建表"><a href="#创建表" class="headerlink" title="创建表"></a><a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/8.0/en/creating-tables.html">创建表</a></h4><pre><code> create table pet (name varchar(20), owner varchar(20), species varchar(20), sex char(1), birth date, death date);</code></pre>
<h4 id="查看表"><a href="#查看表" class="headerlink" title="查看表"></a>查看表</h4><pre><code> show tables;</code></pre>
<h4 id="描述表信息"><a href="#描述表信息" class="headerlink" title="描述表信息"></a>描述表信息</h4><pre><code>describe pet;</code></pre>
<h2 id="数据操作-1"><a href="#数据操作-1" class="headerlink" title="数据操作"></a>数据操作</h2><h4 id="将数据载入表"><a href="#将数据载入表" class="headerlink" title="将数据载入表"></a>将数据载入表</h4><p>有如下数据，保存为 pet.txt. \N 表示 NULL. 数据之间以 tab 间隔.</p>
<hr>
<table>
<thead>
<tr>
<th>name</th>
<th>owner</th>
<th>species</th>
<th>sex</th>
<th>birth</th>
<th>death</th>
</tr>
</thead>
<tbody><tr>
<td>Fluffy</td>
<td>Harold</td>
<td>cat</td>
<td>f</td>
<td>1993-02-04</td>
<td>\N</td>
</tr>
<tr>
<td>Claws</td>
<td>Gwen</td>
<td>cat</td>
<td>m</td>
<td>1994-03-17</td>
<td>\N</td>
</tr>
<tr>
<td>Buffy</td>
<td>Harold</td>
<td>dog</td>
<td>f</td>
<td>1989-05-13</td>
<td>\N</td>
</tr>
<tr>
<td>Fang</td>
<td>Benny</td>
<td>dog</td>
<td>m</td>
<td>1990-08-27</td>
<td>\N</td>
</tr>
<tr>
<td>Bowser</td>
<td>Diane</td>
<td>dog</td>
<td>m</td>
<td>1979-08-31</td>
<td>1995-07-29</td>
</tr>
<tr>
<td>Chirpy</td>
<td>Gwen</td>
<td>bird</td>
<td>f</td>
<td>1998-09-11</td>
<td>\N</td>
</tr>
<tr>
<td>Whistler</td>
<td>Gwen</td>
<td>bird</td>
<td>\N</td>
<td>1997-12-09</td>
<td>\N</td>
</tr>
<tr>
<td>Slim</td>
<td>Benny</td>
<td>snake</td>
<td>m</td>
<td>1996-04-29</td>
<td>\N</td>
</tr>
</tbody></table>
<hr>
<h4 id="载入操作"><a href="#载入操作" class="headerlink" title="载入操作"></a>载入操作</h4><pre><code>load data local infile /mnt/d/pet.txt into table pet;</code></pre>
<h4 id="显示结果"><a href="#显示结果" class="headerlink" title="显示结果"></a>显示结果</h4><pre><code> mysql&gt; load data local infile &#39;/root/pet.txt&#39; into table pet;
 Query OK, 8 rows affected, 48 warnings (0.01 sec)
 Records: 8  Deleted: 0  Skipped: 0  Warnings: 48</code></pre>
<h4 id="查看表中所有数据"><a href="#查看表中所有数据" class="headerlink" title="查看表中所有数据"></a>查看表中所有数据</h4><pre><code> select * from pet;</code></pre>
<h4 id="删除表中所有数据"><a href="#删除表中所有数据" class="headerlink" title="删除表中所有数据"></a>删除表中所有数据</h4><pre><code> delete from pet;</code></pre>
<h2 id="数据查询"><a href="#数据查询" class="headerlink" title="数据查询"></a>数据查询</h2><h4 id="单表查询"><a href="#单表查询" class="headerlink" title="单表查询"></a>单表查询</h4><pre><code>1.  select * from pet where name = &#39;Bowser&#39;;

2.  SELECT * FROM pet WHERE birth &gt;= &#39;1998-1-1&#39;;

3.  SELECT * FROM pet WHERE species = &#39;dog&#39; AND sex = &#39;f&#39;;

4.  SELECT * FROM pet WHERE species = &#39;snake&#39; OR species = &#39;bird&#39;;

5.  SELECT * FROM pet WHERE (species = &#39;cat&#39; AND sex = &#39;m&#39;) OR (species = &#39;dog&#39; AND sex = &#39;f&#39;);</code></pre>
<h4 id="选择特定列"><a href="#选择特定列" class="headerlink" title="选择特定列"></a>选择特定列</h4><pre><code>1.  SELECT name, birth FROM pet;

2.  SELECT owner FROM pet;</code></pre>
<h4 id="去重显示"><a href="#去重显示" class="headerlink" title="去重显示"></a>去重显示</h4><pre><code>1.   SELECT DISTINCT owner FROM pet;</code></pre>
<h4 id="按序显示"><a href="#按序显示" class="headerlink" title="按序显示"></a>按序显示</h4><pre><code>1.  SELECT name, birth FROM pet ORDER BY birth;

2.  SELECT name, birth FROM pet ORDER BY birth DESC;

3.  SELECT name, species, birth FROM pet ORDER BY species, birth DESC;</code></pre>
<h4 id="图案匹配"><a href="#图案匹配" class="headerlink" title="图案匹配"></a>图案匹配</h4><pre><code>‘%’ 表示匹配任意数量的字符。

1.  SELECT * FROM pet WHERE name LIKE &#39;b%&#39;;
2.  SELECT * FROM pet WHERE name LIKE &#39;%fy&#39;;
3.  SELECT * FROM pet WHERE name LIKE &#39;%w%&#39;;

一个 ‘_&#39; 表示一个字符，5个’_&#39; 的字符 &#39;_____&#39; 表示仅仅匹配 5 个字符.

4.  &lt;SELECT * FROM pet WHERE name LIKE &#39;_____&#39;;&gt;</code></pre>
<h4 id="正则匹配"><a href="#正则匹配" class="headerlink" title="正则匹配"></a>正则匹配</h4><pre><code>1.  . 匹配任意单个字符。
2.  [...] 匹配方括号内的任意字符，[abc] 匹配 a, b, c.
3.  * 表示匹配 0 个或 多个 * 之前的字符.
4.  为了定位一个图案的开始和结束字符，^ 在开头， $ 在结尾.

示例：

1.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;^b&#39;);

2.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;^b&#39; COLLATE utf8mb4_0900_as_cs);

3.  SELECT * FROM pet WHERE REGEXP_LIKE(name, BINARY &#39;^b&#39;);

4.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;^b&#39;, &#39;c&#39;);

5.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;fy$&#39;);

6.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;w&#39;);

7.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;^.....$&#39;);    正好匹配5个字符.

8.  SELECT * FROM pet WHERE REGEXP_LIKE(name, &#39;^.&#123;5&#125;$&#39;);    同7.</code></pre>
<h2 id="计数"><a href="#计数" class="headerlink" title="计数"></a>计数</h2><h5 id="查看有多少宠物"><a href="#查看有多少宠物" class="headerlink" title="查看有多少宠物"></a>查看有多少宠物</h5><pre><code>1. SELECT COUNT(*) FROM pet;</code></pre>
<h5 id="查看拥有宠物的人养了多少宠物"><a href="#查看拥有宠物的人养了多少宠物" class="headerlink" title="查看拥有宠物的人养了多少宠物"></a>查看拥有宠物的人养了多少宠物</h5><pre><code>2. SELECT owner, COUNT(*) FROM pet GROUP BY owner;

3. SELECT species, COUNT(*) FROM pet GROUP BY species;

4. SELECT sex, COUNT(*) FROM pet GROUP BY sex;

5. SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;

6. SELECT species, sex, COUNT(*) FROM pet
   WHERE species = &#39;dog&#39; OR species = &#39;cat&#39;
   GROUP BY species, sex;

7. SELECT species, sex, COUNT(*) FROM pet
-&gt; WHERE sex IS NOT NULL
-&gt; GROUP BY species, sex;</code></pre>
<h2 id="多表查询"><a href="#多表查询" class="headerlink" title="多表查询"></a>多表查询</h2><h4 id="创建表-event"><a href="#创建表-event" class="headerlink" title="创建表 event"></a><a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/8.0/en/multiple-tables.html">创建表 event</a></h4><pre><code>create table event (name varchar(20), date date, type varchar(15), remark varchar(255));</code></pre>
<h4 id="载入下表数据"><a href="#载入下表数据" class="headerlink" title="载入下表数据"></a>载入下表数据</h4><table summary="pet record data that will appear in a tab delimited text file, as described in the preceding text."><col width="15%"><col width="15%"><col width="15%"><col width="35%"><thead><tr>
              <th scope="col">name</th>
              <th scope="col">date</th>
              <th scope="col">type</th>
              <th scope="col">remark</th>
            </tr></thead><tbody><tr>
              <td scope="row">Fluffy</td>
              <td>1995-05-15</td>
              <td>litter</td>
              <td>4 kittens, 3 female, 1 male</td>
            </tr><tr>
              <td scope="row">Buffy</td>
              <td>1993-06-23</td>
              <td>litter</td>
              <td>5 puppies, 2 female, 3 male</td>
            </tr><tr>
              <td scope="row">Buffy</td>
              <td>1994-06-19</td>
              <td>litter</td>
              <td>3 puppies, 3 female</td>
            </tr><tr>
              <td scope="row">Chirpy</td>
              <td>1999-03-21</td>
              <td>vet</td>
              <td>needed beak straightened</td>
            </tr><tr>
              <td scope="row">Slim</td>
              <td>1997-08-03</td>
              <td>vet</td>
              <td>broken rib</td>
            </tr><tr>
              <td scope="row">Bowser</td>
              <td>1991-10-12</td>
              <td>kennel</td>
              <td></td>
            </tr><tr>
              <td scope="row">Fang</td>
              <td>1991-10-12</td>
              <td>kennel</td>
              <td></td>
            </tr><tr>
              <td scope="row">Fang</td>
              <td>1998-08-28</td>
              <td>birthday</td>
              <td>Gave him a new chew toy</td>
            </tr><tr>
              <td scope="row">Claws</td>
              <td>1998-03-17</td>
              <td>birthday</td>
              <td>Gave him a new flea collar</td>
            </tr><tr>
              <td scope="row">Whistler</td>
              <td>1998-12-09</td>
              <td>birthday</td>
              <td>First birthday</td>
</tr></tbody></table>

<pre><code>load data local infile &#39;/root/event.txt&#39; into table event;</code></pre>
<h4 id="查询宠物年龄和家庭情况"><a href="#查询宠物年龄和家庭情况" class="headerlink" title="查询宠物年龄和家庭情况"></a>查询宠物年龄和家庭情况</h4><pre><code>select pet.name,
timestampdiff(year,birth,date) as age,
remark
from pet inner join event
on pet.name = event.name
where event.type = &#39;litter&#39;;</code></pre>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://libo0.gitee.io" rel="external nofollow noreferrer">libo</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://libo0.gitee.io/2019/01/13/mysql_database/">http://libo0.gitee.io/2019/01/13/mysql_database/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="http://libo0.gitee.io" target="_blank">libo</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/mysql/">
                                    <span class="chip bg-color">mysql</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban,linkedin,twitter,facebook,google" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            
        </div>
    </div>

    
        <link rel="stylesheet" href="/libs/gitalk/gitalk.css">
<link rel="stylesheet" href="/css/my-gitalk.css">

<div class="card gitalk-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="gitalk-container" class="card-content"></div>
</div>

<script src="/libs/gitalk/gitalk.min.js"></script>
<script>
    let gitalk = new Gitalk({
        clientID: '',
        clientSecret: '',
        repo: '',
        owner: '',
        admin: null,
        id: '2019-01-13T00-00-00',
        distractionFreeMode: false  // Facebook-like distraction free mode
    });

    gitalk.render('gitalk-container');
</script>
    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/01/13/linux%E9%85%8D%E7%BD%AE%E8%BF%9C%E7%A8%8B%E5%85%8D%E5%AF%86%E7%99%BB%E5%BD%95/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/9.jpg" class="responsive-img" alt="linux配置远程免密登录">
                        
                        <span class="card-title">linux配置远程免密登录</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            在本地执行


ssh-keygen -t rsa
这会在用户目录生成 id_rsa 和 id_rsa.pub 两个文件
将 id_rsa.pub 发送到远程要登陆的linux 服务器
scp ./id_rsa.pub  username@
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2019-01-13
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            libo
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/linux/">
                        <span class="chip bg-color">linux</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/01/13/python%E5%8C%85pandas%E5%A4%84%E7%90%86%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/8.jpg" class="responsive-img" alt="python包pandas处理数据学习笔记">
                        
                        <span class="card-title">python包pandas处理数据学习笔记</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            pandas 有两种数据结构： 序列(Series), 数据帧(DataFrame)。数据帧是序列的容器，序列是标量的容器。
for col in df.columns:
    series = df[col]


10 Minutes 
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2019-01-13
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            libo
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                    <a href="/tags/pandas/">
                        <span class="chip bg-color">pandas</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>
<script type="text/javascript" src="/libs/highlight/highlight.pack.js"></script>
<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>

    
<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2020</span>
            <a href="http://libo0.gitee.io" target="_blank">libo</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2020";
                    var startMonth = "3";
                    var startDate = "1";
                    var startHour = "18";
                    var startMinute = "30";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/libo-0" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:libo-0@foxmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>













    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
